<?php
/**
 * 重新提交solution代码，用于教师和出题人员更改已提交代码
 * User: cqfut
 * Date: 2020/2/15
 * Time: 16:10
 */
?>
<script language="Javascript" type="text/javascript" src="../../edit_area/edit_area_full.js"></script>
<?php require("front_view_nav.php"); ?>
<div class="col-lg-offset-1 col-lg-10 col-sm-12 col-xs-12" id="resubmit_solution_code">
    <div class="panel panel-default" style="margin-left: -1.3rem; margin-right: -1.3rem;">
        <div class="panel-body">
            <div class="col-lg-12 col-xs-12 col-sm-12">
                <form action='' method='post' class="form-horizontal" >
                    <fieldset>
                        <legend style="font-size: 1.6rem;">
                            <span><a v-bind:href="problemUrl(problem_id)">题目:&nbsp;{{problem_id}} {{problem_title}}</a> </span>
                            <span style="margin-left: 1.5rem;">评测状态:
                                <span v-bind:style="result_tips_style">{{result_tips}}</span>
                            </span>
                            <span style="margin-left: 1.5rem; color:blue;">
                                <input v-model="enable_o2" type="checkbox" checked><span>O2优化</span>
                            </span>
                            <span style="margin-left: 8rem;">
                                <input @click="retBack" type="button" class="btn" value="返回"/>
                            </span>
                        </legend>
                        <textarea id="solution_code" name="code_txt" style="min-height: 40rem; width: 100%;">
		                </textarea>
                        <p class="col-lg-12 col-sm-12">
                            <span>评测状态:
                                <span v-bind:style="result_tips_style">{{result_tips}}</span>
                            </span>
                            <span class="col-lg-offset-4 col-sm-offset-4">
                                <input @click="pushCode" class="btn btn-primary" type="button" value="提交"/>
                            </span>
                        </p>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>
</div>
<script>
    let app = new Vue({
        el: "#resubmit_solution_code",
        data:{
            solution_id:0,
            problem_id:0,
            problem_title:"",
            language:0,
            result_tips:'未知',
            result_tips_style:'',
            timer:-1,
            enable_o2:1,
        },

        beforeCreate:function () {
            // 初始化代码编辑器
            editAreaLoader.init({
                id: "solution_code",
                start_highlight: true, // if start with highlight
                allow_resize: "both",
                allow_toggle: true,
                word_wrap: true,
                language: "en",
                syntax: "js",
                begin_toolbar: "",
                font_size:12,
                plugins: "",
            });
        },
        mounted:function () {
            let solutionId = getUrlKey('id')
            if ( isNull(solutionId)) {
                window.history.back()
            }
            this.solution_id = solutionId
            this.getSolutionInfo(solutionId)
        },

        methods:{
            retBack:function () {
                window.history.back()
            },
            problemUrl:function (problemId) {
                return "/problem.php?id="+problemId
            },
            updateResultTips:function (result) {
                switch (result) {
                    case 0:
                    case 1:
                        this.result_tips_style = 'color:orangered'
                        this.result_tips = '提交成功,等待评测'
                        break;
                    case 2:
                    case 3:
                        this.result_tips_style = 'color:orangered'
                        this.result_tips = '编译成功'
                        break;
                    case 4:
                        this.result_tips_style = 'color:green'
                        this.result_tips = 'Accpet' // 答案正确
                        break;
                    case 5:
                        this.result_tips_style = 'color:red'
                        this.result_tips = 'PE' // 格式错误
                        break;
                    case 6:
                        this.result_tips_style = 'color:red'
                        this.result_tips = 'WA' // 答案错误
                        break;
                    case 7:
                        this.result_tips_style = 'color:orange'
                        this.result_tips = 'TLE' // 超出时间限制
                        break;
                    case 8:
                        this.result_tips_style = 'color:orange'
                        this.result_tips = 'MLE' // 超出内存限制
                        break;
                    case 9:
                        this.result_tips_style = 'color:orange'
                        this.result_tips = 'OLE' // 输出超限
                        break;
                    case 10:
                        this.result_tips_style = 'color:red'
                        this.result_tips = 'RE' // 运行时错误
                        break;
                    case 11:
                        this.result_tips_style = 'color:red'
                        this.result_tips = 'CE' // 编译错误
                        break;
                    case 12:
                    case 13:
                        this.result_tips_style = 'color:red'
                        this.result_tips = '未知错误'
                        break;
                }
                if ( result >= 13 ) {
                    clearInterval(this.timer)
                }
                if ( result == 4 ) {
                    this.retBack()
                }
            },
            getJudgeStatus:function () {
                let reqData = {op:'query_status', solution_id:this.solution_id}
                let param = "para="+JSON.stringify(reqData)
                new Promise(function(resolve, reject) {
                    $.ajax({
                        type:'post',
                        url:'../model/solution_router.php',
                        data:param,
                        dataType:'json',
                        success:function (retData) {
                            if ( retData.status ) {
                                resolve(retData.dat)
                            }
                            else {
                                reject(retData.msg)
                            }
                        }
                    })
                }).then(function(jsonData) {
                    // 注意：mounted 时,无法使用this
                    app.updateResultTips(jsonData.result)
                }).catch(function(err) {
                    alert(err)
                    console.log('出错：' + err)
                })
            },
            pushCode:function () {
                let code = editAreaLoader.getValue("solution_code")
                // base64加密
                let b64Str = Base64.encode(code);
                // url编码，避免 +、=在传输过程中的干扰
                let urlB64Str = encodeURIComponent(b64Str)
                if ( this.enable_o2 ) {
                    this.enable_o2 = 1
                }else {
                    this.enable_o2 = 0
                }
                let reqData = {
                    solution_id:this.solution_id,
                    source_code:urlB64Str,
                    enable_o2:this.enable_o2,
                    op:'rejudge'}
                console.log(reqData)
                let param = "para="+JSON.stringify(reqData)
                new Promise(function(resolve, reject) {
                    $.ajax({
                        type:'post',
                        url:'../model/solution_router.php',
                        data:param,
                        dataType:'json',
                        success:function (retData) {
                            if ( retData.status ) {
                                resolve(retData.dat)
                            }
                            else {
                                reject(retData.msg)
                            }
                        }
                    })
                }).then(function(jsonData) {
                    // 注意：mounted 时,无法使用this
                    app.updateResultTips(0)
                    app.timer = setInterval(app.getJudgeStatus, 1000)
                }).catch(function(err) {
                    alert(err)
                    console.log('出错：' + err);
                })
            },
            // 获取暂存的代码列表
            getSolutionInfo:function(solutionId) {
                let reqData = {solution_id:solutionId, op:'fetch'};
                let param = "para="+JSON.stringify(reqData)
                new Promise(function(resolve, reject) {
                    $.ajax({
                        type:'post',
                        url:'../model/solution_router.php',
                        data:param,
                        dataType:'json',
                        success:function (retData) {
                            console.log(retData)
                            resolve(retData.dat);
                        }
                    })
                }).then(function(jsonData) {
                    // 注意：mounted 时,无法使用this
                    app.problem_id = jsonData.problem_id
                    app.problem_title = jsonData.problem_title
                    app.language = jsonData.language
                    let code = Base64.decode(jsonData.source_code);
                    editAreaLoader.setValue("solution_code", code)
                }).catch(function(err) {
                    console.log('出错：' + err);
                })
            }
        }
    });
</script>
</body>
</html>
